home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 5852 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.6 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Easiest way to center a string?
  5. Date: Wed, 21 Feb 96 15:08:59 GMT
  6. Organization: none
  7. Distribution: world
  8. Message-ID: <824915339snz@genesis.demon.co.uk>
  9. References: <4fobka$1st@parlor.hiwaay.net> <DMqEG5.8z4@eskimo.com> <Pine.A32.3.91.960221001254.156335E-100000@black.weeg.uiowa.edu>
  10. Reply-To: fred@genesis.demon.co.uk
  11. X-NNTP-Posting-Host: genesis.demon.co.uk
  12. X-Newsreader: Demon Internet Simple News v1.27
  13. X-Mail2News-Path: genesis.demon.co.uk
  14.  
  15. In article <Pine.A32.3.91.960221001254.156335E-100000@black.weeg.uiowa.edu>
  16.            robinson@blue.weeg.uiowa.edu "The Amorphous Mass" writes:
  17.  
  18. >On 20 Feb 1996, Tanmoy Bhattacharya wrote:
  19. >
  20. >> In article Amorphous Mass <robinson@blue.weeg.uiowa.edu> writes: 
  21. >> <snip>
  22. >>      That's equivalent to strlen(String + -LineLen), which is in turn 
  23. >>    equivalent to strlen(String[-LineLen]), and the expression String[-80] is 
  24. >>    portable only in the sense that its behavior is undefined on all 
  25. >>    platforms. :)
  26. >> 
  27. >> Sorry there is no trigraph for `&': so if that key is broken, you just
  28. >> have to get it fixed :-)
  29. >
  30. >???  Works fine.  My tilde key is broken though, which makes UNIX a real 
  31. >pain.
  32.  
  33. String + -LineLen is equivalent to &String[-LineLen] except where
  34. String[-LineLen] tries to access the element one past the end of the
  35. array.
  36.  
  37. >>      If you meant (strlen(String) - LineLen) / 2 then you will get a 
  38. >>    negative length specifier for any string with fewer than LineLen 
  39. >>    characters.  If you meant (LineLen - strlen(String) / 2, then your 
  40. >> 
  41. >> strlen returns size_t which is guaranteed unsigned. (I am assuming
  42. >> LineLen is not long.)
  43. >
  44. >  Right, but strlen(String) would be 12, minus LineLen which is 80, which 
  45. >is -68,
  46.  
  47. No, it is UINT_MAX (or ULONG_MAX, etc.)-68 (assuming the size_t value doesn't
  48. get promoted to int or long).
  49.  
  50. >divided by two is -34, which is the number passed as a width 
  51. >specifier to printf().  I don't think printf("%*s", -34, "hello, world); 
  52. >is worth much. :)
  53.  
  54. Probably more than (UINT_MAX-68)/2 ! :-) Also with fixed string -------^
  55.  
  56. >  I guess I didn't say that at all clearly the first time.  Is it clearer 
  57. >now, or am I _completely_ brain-dead?
  58.  
  59. You don't provide much in the way of choice there! :-))
  60.  
  61. Did anybody mention:
  62.  
  63.   (int)((strlen(String) + LineLen) / 2) ?
  64.  
  65. You just have to decide what to do if the string is longer than the line.
  66.  
  67. -- 
  68. -----------------------------------------
  69. Lawrence Kirby | fred@genesis.demon.co.uk
  70. Wilts, England | 70734.126@compuserve.com
  71. -----------------------------------------
  72.